var result = false;
var obj = localStorage.getItem("data");
//验证手机号
var checkPhone = function(a) {
	var patrn = /^((?:13|15|18|14|17)\d{9}|0(?:10|2\d|[3-9]\d{2})[1-9]\d{6,7})$/;
	if(!patrn.exec(a)) return false;
	return true;
};

//判断前三个形势里程
function mileage(el, img) {
	el.length > 0 ? img.attr("src", "../images/car-icon1.png") : img.attr("src", "../images/index-icon.png");
	el.length > 0 ? Flag = true : Flag = false;
};

//图形验证
window.callback = function(res) {
	var loanTelphone = $('#loanTelphone').val();

	if(res.ret === 0) {

		$.ajax({
			async: false,

			type: "post",
			data: {
				ticket: res.ticket,
				randstr: res.randstr,
				phone: loanTelphone
			},
			url: "/vehicleassess/sendMsg",
			success: function(data) {

				if(data.code === 1001) {
					result = true;
					settime();
				} else {
					layer.msg(data.msg);
				}

			},
			error: function(error) {
				console.log(error);
			}

		});

	}
};

//点击下一步
$('.index-sure').on('click', function() {

	var patten = /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g;
	var patten1 = /^\d{4}-((0([1-9]))|(1(0|1|2)))$/;
	var Oimg = $('.index-ident label img');
	var time = $('#car-time').val();
	var num = $('#car-mileage').val();
	if($(".ident-brand option:selected").text() == '直接选择或搜索选择') {
		layer.msg('请选择车辆品牌！');
	} else {
		if($(".ident-system option:selected").text() == '请选择') {
			layer.msg('请选择车系！');
		} else {

			if($(".ident-type option:selected").text() == '请选择') {
				layer.msg('请选择车型！');
			} else {
				if($(".ident-city option:selected").text() == '请选择') {
					layer.msg('请选择所在城市！');
				} else {
					if(!patten1.test(time)) {
						layer.msg('请选择正确的上牌时间！');
					} else {
						if(!patten.test(num)) {
							layer.msg('请输入正确的行驶里程数！');
						} else {

							$('.mask').css('visibility', 'visible');
							$('.index').css('filter', 'blur(5px)');
						}
					}
				}
			}
		}
	}

});

function myFunction() {
	layer.msg('验证成功即将为你匹配车贷产品！');
	myVar = setTimeout(function() {
		window.location.href = "car/projectCar.html";
	}, 2500);
};

function myStopFunction() {

	clearTimeout(myVar);
};
//提交车辆估值
function addInfo(data1) {

	var brand = $(".ident-brand option:selected").text();
	var series = $(".ident-system option:selected").text();
	var type = $(".ident-type option:selected").text();
	var time = $('#car-time').val();
	var num = $('#car-mileage').val();

	$.ajax({
		async: false,

		type: "post",
		data: {
			brand: brand,
			series: series,
			type: type,
			upTime: time,
			kmNum: num,
			cost: data1.result.result.body.carPrice.price,
			b2c: data1.result.result.body.carPrice.normal.B2C,
			c2b: data1.result.result.body.carPrice.normal.C2B,
			c2c: data1.result.result.body.carPrice.normal.C2C
		},
		url: "/vehicleassess/addInfo",

		success: function(data) {

			if(data.code == 1001) {
				myFunction();
			} else {
				layer.msg(data.msg);
			}

		}

	});
}
//进入手机号验证，验证码验证
$('.submit').on('click', function() {
	var loanTelphone = $('#loanTelphone').val();

	var loanShortYan = $('#loanShortYan').val();

	var autohomeid = $(".ident-type option:selected")[0].id;
	var time = $('#car-time').val();
	var num = $('#car-mileage').val();

	if(!checkPhone(loanTelphone)) {
		layer.msg('请输入正确的手机号！');
	} else {
		if(loanShortYan == '' || result == false) {
			layer.msg('点击获取验证码');
		} else {

			//判断验证码是否正确
			$.ajax({
				async: false,

				type: "post",
				data: {
					code: loanShortYan
				},
				url: "/vehicleassess/verification",
				success: function(data) {

					if(data.code == 1001) {

						layer.msg('验证码成功！');
						//验证码正确进行总的估价
						$.ajax({
							async: false,

							type: "get",
							data: {
								appkey: obj,
								autohomeid: autohomeid,
								regdate: time,
								province: '23',
								city: '385',
								miles: num
							},
							url: "/guguiche/SeniorValuation",

							success: function(data) {
								
								var str = {
									price:  data.result.result.body.carPrice.price,
									B2C: data.result.result.body.carPrice.normal.B2C,
									B2C_max:data.result.result.body.carPrice.normal.B2C_max,
									C2B_min:data.result.result.body.carPrice.normal.C2B_min,
									C2B:data.result.result.body.carPrice.normal.C2B,
									B2C_C2C:data.result.result.body.carPrice.normal.B2C_C2C
								};
								var d = JSON.stringify(str);
								window.localStorage.setItem("data1", d);

								if(data.code == 10000) {
									addInfo(data);
								} else {
									layer.msg('失败的操作！')
								}

							}

						});

					} else {
						layer.msg('验证码错误！');
					}

				},
				error: function(error) {

				}

			});
			//		
			//		//验证码正确进行总的估价

		}

	}

});
//遮罩取消
$(".cancel").on('click', function() {
	$('.mask').css('visibility', 'hidden');
	$('.index').css('filter', 'blur(0px)');

});

function onChange1(el, img) {
	$('.ident-type').empty();
	var systemId = $(".ident-system option:selected")[0].id;

	//请求车系对应的车型列表
	$.ajax({
		async: false,

		type: "get",
		data: {
			appkey: obj,
			familyid: systemId
		},
		url: "/guguiche/dmpdetailVehicleInfo",
		success: function(data) {

			if(data.result.result.code == 200) {

				var arror = data.result.result.result;
				var str = {
					salesdesc: "请选择"
				};
				arror.unshift(str);

				for(var i = 0; i < arror.length; i++) {
					//								
					var html = "<option id=" + arror[i].autohomeid + " >" + arror[i].salesdesc + "</option>";
					$('.ident-type').append(html);
					layui.use('form', function() { //此段代码必不可少
						var form = layui.form;
						form.render();

					});

				}

			}

		}

	});
}
//			//选择品牌时选择对应的车系
function onChange() {

	var id = $(".ident-brand option:selected")[0].id;
	$('.ident-system').empty();

	//发送请求获取对应车系
	$.ajax({
		async: false,

		type: "get",
		data: {
			appkey: obj,
			brandid: id
		},
		url: "/guguiche/dmpfamilyInfo",
		success: function(data) {
		

			if(data.result.result.code == 200) {

				var arror = data.result.result.result;
				var str = {
					familyname: "请选择"
				};
				arror.unshift(str);

				for(var i = 0; i < arror.length; i++) {
					//								
					var html = "<option id=" + arror[i].id+ " >" + arror[i].familyname + "</option>";
					$('.ident-system').append(html);
					layui.use('form', function() { //此段代码必不可少
						var form = layui.form;
						form.render();
						form.on('select(system)', function(data) {

							onChange1();
							data.value != '请选择' ? $('.car-system').attr("src", "../images/car-icon1.png") : $('.car-system').attr("src", "../images/index-icon.png");

						})
					});

				}

			}

		}

	});

}
//车辆品牌
$(function() {

	$.ajax({
		async: false,

		type: "get",

		url: "/vehicleassess/getKey",
		success: function(data) {

			localStorage.setItem('data', data);
		}

	});

	layui.use('form', function() { //此段代码必不可少
		var form = layui.form;
		form.render();
	});
	$.ajax({
		async: false,

		type: "get",
		data: {
			appkey:obj
		},
		url: "/guguiche/dmpAllBrands",
		success: function(data) {
				console.log(data);

			if(data.result.result.code == 200) {

				var arror = data.result.result.result;

				arror.forEach(function(i, item) {
					var htmls = "";

					htmls += "<option id=" + i.id + ">" + i.brandname + "</option>";
					$('.ident-brand').append(htmls);
					layui.use('form', function() { //此段代码必不可少
						var form = layui.form;
						form.render();
						form.on('select(business)', function(data) {

							data.value != '请选择' && '直接选择或搜索选择' ? $('.car-brand').attr("src", "../images/car-icon1.png") : $('.car-brand').attr("src", "../images/index-icon.png");
							onChange();

						});
					});

				});

			}

		}

	});

});